import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import promiseMiddleware from 'redux-async-promise';
import reducers from './src/redux/reducers';

const middleware = [thunk, promiseMiddleware];
const enhancers = [];
if (process.env.NODE_ENV === 'development') {
  const devToolsExtension = window.__REDUX_DEVTOOLS_EXTENSION__;
  if (typeof devToolsExtension === 'function') {
    enhancers.push(devToolsExtension());
  }
}

export function configureStore(initialState = {}) {
  const store = createStore(
    reducers,
    initialState,
    compose(
      applyMiddleware(...middleware),
      ...enhancers,
    ),
  );

  // if (process.env.NODE_ENV === 'development') {
  //   if (module.hot) {
  //     module.hot.accept('./redux/reducers', () => {
  //       // eslint-disable-next-line global-require,import/newline-after-import
  //       const nextReducers = require('./redux/reducers');
  //       store.replaceReducer(nextReducers);
  //     });
  //   }
  // }

  return store;
}

export default configureStore();
